package edu.mju.route.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.mju.route.client.ScenicClient;
import edu.mju.route.entity.Spot;
import edu.mju.route.mapper.SpotMapper;
import edu.mju.route.service.SpotService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.mju.route.vo.ClientSpot;
import edu.mju.route.vo.ReFrontSpot;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author CBL
 * @since 2020-12-30
 */
@Service
public class SpotServiceImpl extends ServiceImpl<SpotMapper, Spot> implements SpotService {

    @Autowired
    private ScenicClient scenicClient;

    @Override
    public Map<String, Object> getRouteSpot(Long page, Long size, Integer routeId) {
        //根据条件查询景点
        IPage<Spot> spotIpage = new Page<>(page,size);
        QueryWrapper<Spot> wrapper = new QueryWrapper<>();
        wrapper.eq("route_id",routeId);
        baseMapper.selectPage(spotIpage,wrapper);

        List<ClientSpot> spotList = new ArrayList<>();
        //查询景点详细信息
        List<Spot> records = spotIpage.getRecords();
        for (Spot spot : records) {
            //远程调用 取出结果
            ClientSpot spotInfo = scenicClient.getSpotInfo(spot.getSpotId());
            spotList.add(spotInfo);

        }

        long total = spotIpage.getTotal();

        Map<String,Object> map = new HashMap<>();
        map.put("total",total);
        map.put("route_id",routeId);
        map.put("spotList",spotList);


        return map;
    }
}
